package com.yj.audio_output_device_new.receiver;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.yj.audio_output_device_new.callback.impl.CallbackImpl;
import com.yj.audio_output_device_new.util.log.Log;
import com.yj.audio_output_device_new.util.permission.PermissionUtil;

import java.lang.ref.WeakReference;

public class BleConnectionReceiver extends BroadcastReceiver {
    private static final String TAG = BleConnectionReceiver.class.getName();

    @SuppressLint({"DefaultLocale", "MissingPermission"})
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action == null) return;
        if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(bluetoothDevice != null){
                if(PermissionUtil.checkBleConnectPermission(context)){
                    Log.d(TAG, String.format("%s %s %d disconnected", bluetoothDevice.getName(), bluetoothDevice.getAddress(), bluetoothDevice.getType()));
                    if(bluetoothDevice.getType() == BluetoothDevice.DEVICE_TYPE_CLASSIC ||
                            bluetoothDevice.getType() == BluetoothDevice.DEVICE_TYPE_DUAL){
                        CallbackImpl.getInstance().onClassicDeviceDisconnected();
                    }
                }

            }

        }
    }
}
